package com.pagoda.nerp.web.services;

import com.pagoda.nerp.web.entities.HRORGPost;
import com.pagoda.nerp.web.entities.HRUnit;
import com.pagoda.nerp.web.repositories.HRORGPostDao;
import com.pagoda.nerp.web.repositories.HRUnitDao;
import com.pagoda.nerp.web.repositories.HRZGZDDao;
import com.pagoda.nerp.web.entities.HRZGZD;
import com.pagoda.nerp.web.repositories.HRZGZDDaoSpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HRZGZDService {
    @Autowired
    private HRZGZDDao hrzgzdDao;
    @Autowired
    private HRUnitDao hrUnitDao;
    @Autowired
    private HRORGPostDao hrorgPostDao;
    @Autowired
    private EHRService ehrService;
    @Autowired
    private UserMappingService userMappingService;

    public void save(List<HRZGZD> list){
        hrzgzdDao.save(list);
    }

    /**
    *  @Description:  获取当前数据库的修改时间
    *  @Pram:
    *  @Date: 9:56 2018/1/3
    */
    public Date getModifyTime(){
        HRZGZD hrzgzd = hrzgzdDao.findByCode("updatetime");

        //return hrzgzd.getModifytime();
        return hrzgzd == null ? new Date(1525276800000L): hrzgzd.getModifytime();
    }

    /**
     *  @Description: 修改用户信息
     *  @Pram:   XML获取的用户实体
     *  @Date: 11:08 2017/12/29
     */
    public void modifyUser(HRZGZD hrzgzd){
        HRUnit hrUnit = hrUnitDao.findByOrgcode(hrzgzd.getUnit());
        HRUnit hrdept = hrUnitDao.findByOrgcode(hrzgzd.getDept());
        HRORGPost hrorgPost = hrorgPostDao.findByJobtitlecode(hrzgzd.getPostcode());

        if(hrUnit!=null){
            hrzgzd.setUnitname(hrUnit.getOrgname());
            hrzgzd.setUnitid(hrUnit.getOrguid());
        }
        if(hrdept!=null) {
            hrzgzd.setDeptname(hrdept.getOrgname());
            hrzgzd.setDeptid(hrdept.getOrguid());
            hrzgzd.setUnitdeptname(hrdept.getOrgalias());
            hrzgzd.setCanonicalcode(hrdept.getCanonicalcode());
        }
        if(hrorgPost!=null){
            hrzgzd.setPostname(hrorgPost.getJobtitlename());
            hrzgzd.setPostid(hrorgPost.getPostid()); }
    }

    /**
    *  @Description: 获取更新的用户名单
    *  @Pram:
    *  @Date: 14:33 2018/1/3
    */
    public List<HRZGZD> getUpdateList(Date modifydate){
        List<HRZGZD> list = new ArrayList<>();

        Calendar current = Calendar.getInstance();
        Calendar modify  = Calendar.getInstance();
        modify.setTime(modifydate);

        while(modify.before(current))
        {
            DateFormat df = new SimpleDateFormat("yyyyMMdd");
            String date = df.format(modify.getTime());
            System.out.println("HRZGZDService.getUpdateList " + date);
            List<HRZGZD> add = ehrService.getHRZGZDList(date);
            if(add !=null) list.addAll(add);
            modify.add(Calendar.DAY_OF_YEAR,1);
        }
        return list;
    }

    public void modifyUpdateTime(){
        Calendar calendar = Calendar.getInstance();
        HRZGZD hrzgzd = hrzgzdDao.findOne("updatetime");
        hrzgzd.setModifytime(calendar.getTime());
        hrzgzdDao.save(hrzgzd);
    }
    /**
    *  @Description: 更新数据库
    *  @Pram:
    *  @Date: 14:34 2018/1/3
    */
    public List<HRZGZD> updateDBEmployee(){
        Date modifydate = getModifyTime();
        List<HRZGZD> updatelist = getUpdateList(modifydate);
        for (HRZGZD hrzgzd : updatelist){
            modifyUser(hrzgzd);
        }
        hrzgzdDao.save(updatelist);
        modifyUpdateTime();

        System.out.println("UpdateDBEmployee Complete!");
        return updatelist;
    }

    /**
    *  @Description: 分页查找用户信息
    *  @Pram: page pagesize 分页信息
     * @Pram: unitid 机构ID postid 岗位ID name 姓名 code 编号 mobile 手机 查询条件
    *  @Date: 17:20 2017/12/28
    */
    public Page<HRZGZD> getHRZGZDInfo(int page,int pagesize,String unitid,String postid,String name,String code,String mobile){
        Set<String> deptSet = null;
        Set<String> jobtitlecodes = null;
        //通过机构ID获取下级机构的编码
        if(null != unitid && !unitid.equals(""))
            deptSet = hrUnitDao.findByCanonicalcodeLike(unitid);

        //通过岗位ID获取岗位编码
        if(null != postid && !postid.equals(""))
            jobtitlecodes = hrorgPostDao.getJobtitlecode(postid);

     //   System.out.println(deptSet.toString()+"\r\n"+jobtitlecodes);
        //设置分页打包工具
        Pageable pageable = new PageRequest(page,pagesize,new Sort(Sort.Direction.ASC,"name"));
        //通过DAO获取到所需数据
        Page<HRZGZD> list = hrzgzdDao.findAll(HRZGZDDaoSpec.getSpec(deptSet,jobtitlecodes,name,code,mobile),pageable);
        return list;
    }

    /**
    *  @Description:  修改用户信息
    *  @Pram:  传入信息
    *  @Date: 10:56 2017/12/29
    */
    public void modifyUser(String code ,String unitid,String deptid,String postid){
        //通过传入用户信息code unitid deptid postid，获取到用户当前信息
        HRUnit unit = hrUnitDao.findOne(unitid);
        HRUnit dept = hrUnitDao.findOne(deptid);
        HRORGPost post = hrorgPostDao.findOne(postid);

        HRZGZD hrinfo = hrzgzdDao.findByCode(code);
        //修改用户信息
        //修改部门信息
        hrinfo.setUnitid(unitid);
        hrinfo.setUnitname(unit.getOrgname());
        hrinfo.setUnit(unit.getOrgcode());

        //修改单位信息
        hrinfo.setDeptid(deptid);
        hrinfo.setDeptname(dept.getOrgname());
        hrinfo.setDept(dept.getOrgcode());
        hrinfo.setUnitdeptname(dept.getOrgalias());
        hrinfo.setCanonicalcode(dept.getCanonicalcode());

        //修改岗位信息
        hrinfo.setPostid(postid);
        hrinfo.setPostname(post.getJobtitlename());
        hrinfo.setPostcode(post.getJobtitlecode());

        //执行更新操作
        hrzgzdDao.save(hrinfo);
    }

    public Page<HRZGZD> test(Set<String> deptSet,Set<String> jobtitlecodes,String name,String code,String mobile){
        Pageable pageable = new PageRequest(0,10);
        return hrzgzdDao.findAll(HRZGZDDaoSpec.getSpec(deptSet,jobtitlecodes,name,code,mobile),pageable);
    }
}
